{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python函数\n",
    "**本章重点:**\n",
    "* 掌握函数参数传递\n",
    "* 变量的作用域\n",
    "* lambda函数\n",
    "\n",
    "函数定义：\n",
    "\n",
    "def functionname([ parameterlist]): \n",
    "      \n",
    "      ['''comments'''] \n",
    "      \n",
    "      [functionbody]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 一、变量的作用域\n",
    "作用域顾名思义是指变量能起作用的区域\n",
    "* 局部变量:\n",
    ">函数内部的变量与函数外部的变量有着不同的储存地址不同的储存空间,函数内部的变量只在函数内部起作用不能在外部使用.当调用函数时候编译器才会给函数内部变量开辟内存空间,函数结束时内存自动释放.\n",
    "* 全局变量:\n",
    ">在函数外部的变量叫做全局变量,或者在函数内部变量前加上global将局部变量变成全局变量,全局变量所有函数都能够使用.且如果函数内部的变量与外部变量有相同的变量名,由于储存空间和内存地址不同改变函数内部的变量不会改变函数外部的变量,除非将其变成全局变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "局部变量PatialVariable:青山遮不住\n",
      "局部变量GlobalVariable:毕竟东流去\n",
      "输出函数调用后的全局变量GlobalVariable:一蓑烟雨任平生\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'PartialVariable' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-25-f6eb1ea5f97e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      8\u001b[0m \u001b[0mfunction\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'输出函数调用后的全局变量GlobalVariable:'\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mGlobalVariable\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 10\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'试图输出局部变量PartialVariable:'\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mPartialVariable\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     11\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     12\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'PartialVariable' is not defined"
     ]
    }
   ],
   "source": [
    "GlobalVariable = '一蓑烟雨任平生'\n",
    "def function():\n",
    "    PartialVariable = '青山遮不住'\n",
    "    #试图在函数内部改变全局变量\n",
    "    GlobalVariable = '毕竟东流去'\n",
    "    print('局部变量PatialVariable:'+PartialVariable)\n",
    "    print('局部变量GlobalVariable:'+GlobalVariable)\n",
    "function()      \n",
    "print('输出函数调用后的全局变量GlobalVariable:'+GlobalVariable)\n",
    "print('试图输出局部变量PartialVariable:'+PartialVariable)\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 很显然局部变量只在内部起作用且如果在函数内部有与全局变量同名的变量在函数内部改变不会影响到全局变量的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "调用函数前的Globalvariable:一蓑烟雨任平生\n",
      "输出global PatialVariable:青山遮不住\n",
      "输出global GlobalVariable:毕竟东流去\n",
      "函数外面输出函数调用后变量GlobalVariable:毕竟东流去\n",
      "函数挖输出变量PartialVariable:青山遮不住\n"
     ]
    }
   ],
   "source": [
    "GlobalVariable = '一蓑烟雨任平生'\n",
    "print('调用函数前的Globalvariable:'+GlobalVariable)\n",
    "def function():\n",
    "    global PartialVariable\n",
    "    PartialVariable = '青山遮不住'\n",
    "    #在函数内部改变全局变量\n",
    "    global GlobalVariable \n",
    "    GlobalVariable = '毕竟东流去'\n",
    "    print('输出global PatialVariable:'+PartialVariable)\n",
    "    print('输出global GlobalVariable:'+GlobalVariable)\n",
    "function()      \n",
    "print('函数外面输出函数调用后变量GlobalVariable:'+GlobalVariable)\n",
    "print('函数外输出函数内变量PartialVariable:'+PartialVariable)\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">显然加了global即声明了把函数内部的变量变成了全局变量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 二、函数参数传递:\n",
    "* 了解形式参数和实际参数:\n",
    "> 形式参数是定义函数时候函数名括号内的参数,而实际参数是在调用函数时的参数,函数的参数传递分为值传递和地址传递,所谓值传递就是在调用函数传参的时候将实际变量的值拷贝到形式参数里面,在函数内改变实际参数的值不会改变实际参数的值,而地址传递是指将实际参数的存储地址传递给形式参数,这时候因为形式参数和实际参数的引用指向同一个地址因此改变形式参数会影响得到实际参数的值,可变值类型是址传递,不可变类型是值传递.\n",
    "* 位置参数:\n",
    "\n",
    "1.传递参数时候在没有设置默认值的情况下必须保证实际参数个数与形式参数个数一致\n",
    "\n",
    "2.传递参数时候位置必须与形式参数对应,否则会出现两种错误,抛出TypeError异常和产生结果与预期不一致\n",
    "\n",
    "* 关键字参数:\n",
    ">关键字参数是指用形式参数name来输入参数值这时候可以以任何顺序输入参数\n",
    "* 为参数设置默认值:\n",
    ">形式参数设置默认值了以后再调用函数传递参数的时候就可以省略掉设置了默认参数值的形式参数而不抛出异常\n",
    "* 可变参数:\n",
    ">可变参数是指\n",
    "* 返回值\n",
    ">函数中加入语句return"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "传递之前: string=amazing num= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
      "传递之后: string=amazing num= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "def function(string,num):\n",
    "    string = 'surprising'\n",
    "    num +=num\n",
    "string = 'amazing'\n",
    "nnum = [x for x in range(10)]\n",
    "print('传递之前:','string='+string,'num=',num)\n",
    "function(string,num)\n",
    "print('传递之后:','string='+string,'num=',num)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 数值类型和元组都是不可变类型,字典列表都是可变类型,上面的字符串类型是值传递,而可变列表是址传递"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "陆小凤的BMI指数为:40.123456790123456\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "位置参数:需要按照实际参数与形式参数顺序需一致\n",
    "以计算某人的BMI指数为例子\n",
    "'''\n",
    "def function(person,weight,height):\n",
    "    BMI = weight/(height*height)\n",
    "    print(person+'的BMI指数为:'+str(BMI))    \n",
    "function('陆小凤',130,1.80)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for /: 'str' and 'float'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-ac6a152db519>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 交换person和weight值\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mfunction\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m130\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'陆小凤'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1.80\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-2-c1ec9c54a6e6>\u001b[0m in \u001b[0;36mfunction\u001b[1;34m(person, weight, height)\u001b[0m\n\u001b[0;32m      4\u001b[0m '''\n\u001b[0;32m      5\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mfunction\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mperson\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mweight\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mheight\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m     \u001b[0mBMI\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mweight\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mheight\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mheight\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      7\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mperson\u001b[0m\u001b[1;33m+\u001b[0m\u001b[1;34m'的BMI指数为:'\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mBMI\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m \u001b[0mfunction\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'陆小凤'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m130\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1.80\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for /: 'str' and 'float'"
     ]
    }
   ],
   "source": [
    "# 交换person和weight值出现TypeError错误\n",
    "function(130,'陆小凤',1.80)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "陆小凤的BMI指数为:0.00010650887573964498\n"
     ]
    }
   ],
   "source": [
    "# 交换weight和height顺序后计算结果与实际情况不一致\n",
    "function('陆小凤',1.80,130)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "function() missing 1 required positional argument: 'height'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-5-18009db2c977>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 省略某个参数\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mfunction\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'陆小凤'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m130\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: function() missing 1 required positional argument: 'height'"
     ]
    }
   ],
   "source": [
    "# 形式参数没有设置默认值时省略某个参数出错\n",
    "function('陆小凤',130)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "陆小凤的BMI指数为:40.123456790123456\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "关键字传参即用形式参数的参数名字传参的方式\n",
    "这时候参数的顺序可以任意\n",
    "'''\n",
    "function(height=1.80,weight=130,person='陆小凤')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "为参数设置默认值\n",
    "'''\n",
    "def defaultfun(a,b,c=1):\n",
    "    print(a+b+c)\n",
    "defaultfun(a=1,b=1)  #可省略c传参"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "variable的类型为: <class 'tuple'>\n",
      "variable的总和为: 28\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "可变参数传参:\n",
    "1.形式参数指定为*variable,这时候可将多个实际参数做成一个tuple传给形式参数\n",
    "2.形式参数指定为**variable,这是将多个关键字传递的参数做成一个dict传给形式参数\n",
    "'''\n",
    "def MultiFun(*variable):\n",
    "    print('variable的类型为:',type(variable))\n",
    "    print('variable的总和为:',sum(variable))\n",
    "MultiFun(1,2,3,4,5,6,7)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3, 4, 5) 4 2\n"
     ]
    }
   ],
   "source": [
    "def MultiFun(*variable,a=1,b):\n",
    "    print(variable,a,b)\n",
    "MultiFun(1,2,3,4,5,a=4,b=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "MultiFun() got an unexpected keyword argument 'variable'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-19-5c9f755a3c78>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m#可变参数不可用参数名传参\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mMultiFun\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvariable\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: MultiFun() got an unexpected keyword argument 'variable'"
     ]
    }
   ],
   "source": [
    "#可变参数不可用参数名传参\n",
    "MultiFun(variable=1,a=2,b=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "MultiFun() missing 1 required keyword-only argument: 'b'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-21-e48c708da2a5>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mMultiFun\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: MultiFun() missing 1 required keyword-only argument: 'b'"
     ]
    }
   ],
   "source": [
    "MultiFun(1,2,3) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3) 1 1\n",
      "(1,) 2 2\n"
     ]
    }
   ],
   "source": [
    "#当*variable参数位于最前面时其他参数必须使用关键字参数传参\n",
    "def MultiFun(*variable,a=1,b=1):\n",
    "    print(variable,a,b)\n",
    "MultiFun(1,2,3)\n",
    "MultiFun(1,a=2,b=2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "non-default argument follows default argument (<ipython-input-26-8846f542c982>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-26-8846f542c982>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    def MultiFun(a=1,b,*variable):\u001b[0m\n\u001b[1;37m                ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m non-default argument follows default argument\n"
     ]
    }
   ],
   "source": [
    "def MultiFun(a=1,b,*variable):\n",
    "    print(variable,a,b)\n",
    "MultiFun(a=1,b=2,3,4,5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">上面出现语法错误说明关键字参数必须位于位置参数前面"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3,) 1 2\n"
     ]
    }
   ],
   "source": [
    "#正确方式为\n",
    "def MultiFun(a,b=1,*variable):\n",
    "    print(variable,a,b)\n",
    "MultiFun(1,2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "{'a': 1, 'b': 2, 'c': 3}\n"
     ]
    }
   ],
   "source": [
    "# 当所传参数是一个字典的时候需要用**variable\n",
    "def dictfunction(**variable):\n",
    "    print(type(variable))\n",
    "    print(variable)\n",
    "dictfunction(a=1,b=2,c=3)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-37-77626ace6bff>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-37-77626ace6bff>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    def dictfunction(**variable,a,b=1,*Variable):\u001b[0m\n\u001b[1;37m                               ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    " def dictfunction(**variable,a,b=1,*Variable):\n",
    "    print(variable,a,b)\n",
    "dictfunction(c=1,a=2,b=3,2,2,3)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 (3, 2, 2, 3) {'d': 4, 'f': 4, 'c': 4}\n"
     ]
    }
   ],
   "source": [
    "idef dictfunction(a,b=1,*Variable,**variable):\n",
    "     print(a,b,Variable,variable)\n",
    "dictfunction(1,2,3,2,2,3,c=4,d=4,f=4) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">可变参数的字典形式必须位于最右端,列表形式的多变参数位于位置参数或者关键字参数之前时其他参数必须用关键字参数形式传参才会正确"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
      "(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\n",
      "{0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8, 8: 9, 9: 10}\n"
     ]
    }
   ],
   "source": [
    "# 将一个列表或者字典或者元组传递给一般参数\n",
    "def function(variable1):\n",
    "    print(variable1)\n",
    "list = [i for i in range(10)]\n",
    "tuple = tuple(i+1 for i in range(10))\n",
    "dict = {i:i+1 for i in range(10)}\n",
    "function(list)\n",
    "function(tuple)\n",
    "function(dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将一个列表、字典或者元组传递给可变参数\n",
    "def function(*variable1):\n",
    "    print(variable1)\n",
    "list = [i for i in range(10)]\n",
    "tuple = tuple(i+1 for i in range(10))\n",
    "dict = {i:i+1 for i in range(10)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([0, 1, 2, 3, 4, 5, 6, 7, 8, 9],)\n"
     ]
    }
   ],
   "source": [
    "function(list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "((1, 2, 3, 4, 5, 6, 7, 8, 9, 10),)\n"
     ]
    }
   ],
   "source": [
    "function(tuple)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "({0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8, 8: 9, 9: 10},)\n"
     ]
    }
   ],
   "source": [
    "function(dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)\n",
      "(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\n"
     ]
    }
   ],
   "source": [
    "function(*list)\n",
    "function(*tuple)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)\n"
     ]
    }
   ],
   "source": [
    "#字典键打包作成元组\n",
    "function(*dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">从上面例子可以看出当把一个list,tuple,dict的实际参数传递给可变参数*variable的时候是将list,tuple,dict(其键)当做一个整体打包作成一个元组,而要想只取出list,tuple,dict中的值就需要解包即*list、*tuple、*dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "function() takes 0 positional arguments but 1 was given",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-50-bb64e41d0e07>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      4\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvariable1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mdict\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m100\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mfunction\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdict\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: function() takes 0 positional arguments but 1 was given"
     ]
    }
   ],
   "source": [
    "# 将字典中的键值对传递给可变参数**variable作成一个字典时候也需要解包**dict\n",
    "import random\n",
    "def function(**variable1):\n",
    "    print(variable1)\n",
    "dict = {i:random.randint(10,100) for i in range(10)}\n",
    "function(dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">可以看到直接传参会出错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "function() keywords must be strings",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-56-fc076f460804>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      4\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvariable\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mdict\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m100\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mfunction\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m**\u001b[0m\u001b[0mdict\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: function() keywords must be strings"
     ]
    }
   ],
   "source": [
    "# 正确方式\n",
    "import random\n",
    "def function(**variable):\n",
    "    print(variable)\n",
    "dict = {i:random.randint(10,100) for i in range(10)}\n",
    "function(**dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">可以看到上面出错TypeError: function() keywords must be strings,提示出错原因键必须是字符串类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'英语': 70, '数学': 150}\n",
      "{'英语': 70, '数学': 150}\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "def function(**variable):\n",
    "    print(variable)\n",
    "dict = {'数学':150,'英语':70}\n",
    "print(dict)\n",
    "function(**dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">将一个字典传递给可变参数时候那个dict.keys必须是string内心才可以"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "function() got an unexpected keyword argument 'd'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-85-c8ea739e8870>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[0mc\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfunction\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m**\u001b[0m\u001b[0mdict2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0md\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfunction\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m**\u001b[0m\u001b[0mdict1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      9\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0md\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: function() got an unexpected keyword argument 'd'"
     ]
    }
   ],
   "source": [
    "# 函数中return函数的使用\n",
    "def function(a,b):\n",
    "    return a+b\n",
    "dict1 = {'c':1,'d':2} #字典的键与形式参数不一致\n",
    "dict2 = {'b':1,'a':2} #子弹的键与形式参数一致\n",
    "c = function(**dict2)\n",
    "print(c)\n",
    "d = function(**dict1)\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">由上面的例子可以看出在传递参数时解包字典**dict={'a':1,'b':c}实际上是将键值对转化成了等式a=1,b=2在传递参数的时候就相当于是传递了a=1,b=2两个参数即function(**dict)=function(a=1,b=2)因此字典键值不能是数字型必须是字符串形式不然会出错,而且当形式参数是function(variable1,variable2)时候其variable1、variable2也要与dict的键名相同才可以."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a b\n"
     ]
    }
   ],
   "source": [
    "#如下可见*dict其实是提取出dict的键\n",
    "dict = {'a':1,'b':2}\n",
    "print(*dict)   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('机器学习', 48, 100), ('中国通史', 68, 120), ('人类简史', 68, 100)]\n"
     ]
    }
   ],
   "source": [
    "# lambda表达式的使用\n",
    "bookinfo = [('人类简史',68,100),('中国通史',68,120),('机器学习',48,100)]\n",
    "bookinfo.sort(key=lambda x:(x[1],x[1]/x[2]))\n",
    "print(bookinfo)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">key = lambda x:(x[1],x[1]/x[2])是指定列表的排序方式为优先每个元素的第二个位置的值的大小按升序排列,其次按第一个元素除上第二个元素升序排列\n",
    "lambda variable1.....:expression\n",
    "variable1.....表示要传递的参数,expression表示表达式,lambda函数能够返回一个值必须要将返回值赋值给变量,一般这样的匿名函数用于需要几句话处理且一次性使用的功能."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
